package com.neborosoft.android.gui;

import android.view.MenuItem;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 03.03.13
 * Time: 21:11
 * To change this template use File | Settings | File Templates.
 */
public class AndroidMenuManager {
    private static AndroidMenuManager instance;

    private Map<Integer,OnMenuItemClick> onMenuItemClickListeners = new HashMap();

    public static interface OnMenuItemClick{
        void onClick(MenuItem menuItem);
    }

    public static AndroidMenuManager getInstance(){
        if(instance == null){
            instance = new AndroidMenuManager();
        }

        return instance;
    }

    public void setOnMenuItemClickListener(int menuItemId, OnMenuItemClick listener){
        onMenuItemClickListeners.put(menuItemId, listener);
    }

    public OnMenuItemClick getOnMenuItemClickListener(int menuItemId){
        return onMenuItemClickListeners.get(menuItemId);
    }

    public boolean onOptionsItemSelected(MenuItem menuItem){
        int id = menuItem.getItemId();
        OnMenuItemClick onMenuItemClick = onMenuItemClickListeners.get(id);
        if(onMenuItemClick != null){
            onMenuItemClick.onClick(menuItem);
            return true;
        }

        return false;
    }
}
